#player movement
import pygame
from supp import import_folder

class Player(pygame.sprite.Sprite):
    def __init__(self, pos):
        super().__init__()
        self.import_character_assets()
        self.frame_index = 0
        self.animation_speed = 0.15 # fps
        self.image = self.animations['idle'][self.frame_index] # get the image from the file
        self.rect = self.image.get_rect(topleft = pos)

        # player movement
        self.direction = pygame.math.Vector2(0,0)
        self.speed = 2

        # player status
        self.facing_right = True

        # collision initialize
        self.on_ground = False
        self.on_ceiling = False
        self.on_left = False
        self.on_right = False

    # use this function to load the picture in a certain file
    def import_character_assets(self):
        character_path = '../graphics/character/'
        self.animations = {'idle':[]}
        
        for animation in self.animations.keys():
            full_path = character_path + animation
            self.animations[animation] = import_folder(full_path)

    def animate(self):
        # this part can be used when the player has a series of image to make an animation
        animation = self.animations['idle']

        # loop over frame index
        self.frame_index += self.animation_speed
        if self.frame_index >= len(animation):
            self.frame_index = 0
        
        image = animation[int(self.frame_index)]
        if self.facing_right:
            self.image = image
        else:
            flipped_image = pygame.transform.flip(image,True, False) # flip in x axis
            self.image = flipped_image

        # check the position with moving direction
        # set the player on the correct place in the player.rect
        if self.on_ground and self.on_right:
            self.rect = self.image.get_rect(bottomright = self.rect.bottomright)
        elif self.on_ground and self.on_left:
            self.rect = self.image.get_rect(bottomleft = self.rect.bottomleft)
        elif self.on_ground:
            self.rect = self.image.get_rect(midbottom = self.rect.midbottom)
        elif self.on_ceiling and self.on_right:
            self.rect = self.image.get_rect(topright = self.rect.topright)
        elif self.on_ceiling and self.on_left:
            self.rect = self.image.get_rect(topleft = self.rect.topleft)
        elif self.on_ceiling:
            self.rect = self.image.get_rect(midtop = self.rect.midtop)

    # movement control   
    def get_input(self):
        keys = pygame.key.get_pressed()

        # key control
        # use elif to avoid pressing two keys at the same time, which may lead to error
        # End
        if keys[pygame.K_RIGHT]:
            self.direction.x = 1
            self.direction.y = 0
            self.facing_right =True
        # Home
        elif keys[pygame.K_LEFT]:
            self.direction.x = -1
            self.direction.y = 0
            self.facing_right = False 
        # PgUp  
        elif keys[pygame.K_UP]:
            self.direction.y = -1
            self.direction.x = 0
        # PgDn
        elif keys[pygame.K_DOWN]:
            self.direction.y = 1
            self.direction.x = 0
        # no pressing
        else:
            self.direction.x = 0
            self.direction.y = 0
    
    # update the screen
    def update(self):
        self.get_input()
        self.animate()
        self.rect.x += self.direction.x * self.speed
        self.rect.y += self.direction.y * self.speed